import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/* MORTGAGE OR NOT Option whether its rail
 * property, and utility  */

public class CustomizePropertyPanel extends JFrame{
	private Property prop;
	private Player currentPlayer;
	private Rail rail;
	private Utility util;
	private JLabel label;
	private playerPanelUi playPanel;
	private JButton buy, sell, mortgage, unmortgage;

	
	public CustomizePropertyPanel(Block bl_, playerPanelUi playPanel_){
		this.playPanel=playPanel_;
		
		if(bl_ instanceof Property){
			this.prop = (Property)bl_;
		}else if(bl_ instanceof Utility){
			this.util = (Utility)bl_;
		}else if(bl_ instanceof Rail){
			this.rail = (Rail)bl_;
		}
		
		initComponents();
	}
	
	public void initComponents(){
		setTitle("MonoFilpoly Board Game");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(10,2, 320, 240);
		this.setVisible(true);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setBackground(new Color(216, 191, 216));
		
		contentPane.setVisible(true);
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		
		if(this.prop==null && this.rail==null){
			label = new JLabel("");
			label.setIcon(new ImageIcon(getClass().getResource(this.util.getUtilityDeed())));
			label.setBounds(0, 0, 199, 200);
			label.setVisible(true);
			this.add(label);
		}else if(this.rail==null && this.util==null){
			label = new JLabel("");
			label.setIcon(new ImageIcon(getClass().getResource(this.prop.getPropertyDeed())));
			label.setBounds(0, 0, 199, 200);
			label.setVisible(true);
			System.out.println("Made it here");
			this.add(label);
		}else if(this.prop==null && this.util==null){
			label = new JLabel("");
			label.setIcon(new ImageIcon(getClass().getResource(this.rail.getRailDeed())));
			label.setBounds(0, 0, 199, 200);
			label.setVisible(true);
			this.add(label);
		}
		
		
		
		mortgage = new JButton("Mortgage");
		mortgage.setFont(new Font("Tahoma", Font.BOLD, 11));
		mortgage.setBounds(199, 68, 110, 30);
		mortgage.setEnabled(false);
		mortgage.setVisible(false);
		mortgage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mortgage();
			}
		});
		this.add(mortgage);
		
		unmortgage = new JButton("Unmortgage");
		unmortgage.setFont(new Font("Tahoma", Font.BOLD, 11));
		unmortgage.setBounds(199, 95, 110, 30);
		unmortgage.setEnabled(false);
		unmortgage.setVisible(false);
		unmortgage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				unmortgage();
			}
		});
		this.add(unmortgage);
		
		showProperButtons();
	}
	
	public void mortgage(){
		if(this.prop==null && this.rail==null){//util
			this.util.setIsMortgaged(true);
			this.currentPlayer=this.util.getOwner();
			this.currentPlayer.addMoney(this.util.getMortgage());
			JOptionPane.showMessageDialog(null,"Your property has been mortgaged! You received "+this.util.getMortgage()+".");
			this.playPanel.updatePlayerMoney();
			this.dispose();
		}else if(this.rail==null && this.util==null){//prop
			this.prop.setIsMortgaged(true);
			this.currentPlayer=this.prop.getOwner();
			this.currentPlayer.addMoney(this.prop.getMortgage());
			JOptionPane.showMessageDialog(null,"Your property has been mortgaged! You received "+this.prop.getMortgage()+".");
			this.playPanel.updatePlayerMoney();
			this.dispose();
		}else if(this.prop==null && this.util==null){//rail
			this.rail.setIsMortgaged(true);
			this.currentPlayer=this.rail.getOwner();
			this.currentPlayer.addMoney(this.rail.getMortgage());
			JOptionPane.showMessageDialog(null,"Your property has been mortgaged! You received "+this.rail.getMortgage()+".");
			this.playPanel.updatePlayerMoney();
			this.dispose();
		}
	}
	
	public void unmortgage(){
		if(this.prop==null && this.rail==null){//util
			this.util.setIsMortgaged(false);
			this.currentPlayer=this.util.getOwner();
			this.currentPlayer.subtractMoney((int)(this.util.getMortgage()+(this.util.getMortgage()*.1)));
			JOptionPane.showMessageDialog(null,"Your property has been UNmortgaged! You spent "+(this.util.getMortgage()+(this.util.getMortgage()*.1))+".");
			this.playPanel.updatePlayerMoney();
			this.dispose();
		}else if(this.rail==null && this.util==null){//prop
			this.prop.setIsMortgaged(false);
			this.currentPlayer=this.prop.getOwner();
			this.currentPlayer.subtractMoney((int)(this.prop.getMortgage()+(this.prop.getMortgage()*.1)));
			JOptionPane.showMessageDialog(null,"Your property has been UNmortgaged! You spent "+(this.prop.getMortgage()+(this.prop.getMortgage()*.1))+".");
			this.playPanel.updatePlayerMoney();
			this.dispose();
		}else if(this.prop==null && this.util==null){//rail
			this.rail.setIsMortgaged(false);
			this.currentPlayer=this.rail.getOwner();
			this.currentPlayer.subtractMoney((int)(this.rail.getMortgage()+(this.rail.getMortgage()*.1)));
			JOptionPane.showMessageDialog(null,"Your property has been UNmortgaged! You spent "+(this.rail.getMortgage()+(this.rail.getMortgage()*.1))+".");
			this.playPanel.updatePlayerMoney();
			this.dispose();
		}
	}
	
	public void showProperButtons(){
		if(this.prop==null && this.rail==null){//util
			if(this.util.isIsMortgaged()==false){
				mortgage.setVisible(true);
				mortgage.setEnabled(true);
			}else{
				unmortgage.setVisible(true);
				unmortgage.setEnabled(true);
			}
			
		}else if(this.rail==null && this.util==null){//prop
			if(this.prop.isIsMortgaged()==false){
				mortgage.setVisible(true);
				mortgage.setEnabled(true);
			}else{
				unmortgage.setVisible(true);
				unmortgage.setEnabled(true);
			}
			
		}else if(this.prop==null && this.util==null){//rail
			if(this.rail.isIsMortgaged()==false){
				mortgage.setVisible(true);
				mortgage.setEnabled(true);
			}else{
				unmortgage.setVisible(true);
				unmortgage.setEnabled(true);
			}
			
		}
	}
}
